package org.wsjy.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.wsjy.mapper.TClassroomMapper;
import org.wsjy.model.PageList;
import org.wsjy.model.TClassroom;
import org.wsjy.model.TClassroomExample;
import org.wsjy.model.TClassroomExample.Criteria;

/**
 * 
 * @Title:
 * @Company:万世极缘工作室
 * @Description:
 * @Author:万世极缘
 * @Since:2016年2月19日
 * @Copyright:Copyright (c) 2016
 * @ModifyDate:
 * @Version:1.1.0
 */
@Service
public class ClassroomService {
    @Autowired
    private TClassroomMapper classroomMapper;

    /**
     * 
     * @param stSign
     * @param clContent
     * @param page
     * @param pageSize
     * @param sort
     * @return
     * @Description:
     */
    public PageList<TClassroom> findPageList(String stSign, String stTitle, int page, int pageSize, String sort) {
        // 拿到条件的总数
        TClassroomExample classroomExample = new TClassroomExample();
        Map<String, Object> conditionMap = new HashMap<String, Object>();
        Criteria criteria = classroomExample.createCriteria();
        if (StringUtils.isNotEmpty(stSign)) {
            criteria.andStSignEqualTo(stSign);
            conditionMap.put("stSign", stSign);
        }
        if (StringUtils.isNotEmpty(stTitle)) {
            criteria.andStTitleLike(stTitle);
            conditionMap.put("stTitle", stTitle);
        }
        int total = classroomMapper.countByExample(classroomExample);
        int pages = total % pageSize == 0 ? total / pageSize : (total / pageSize + 1);
        // / 如果当前页大于pages，将当前页转成最大页
        if (page > pages && pages > 0) {
            page = pages;
        }
        PageList<TClassroom> classroomPageList = new PageList<TClassroom>();
        int start = (page - 1) * pageSize;
        List<TClassroom> classroomList = classroomMapper.findByPage(stSign, stTitle, start, pageSize, sort);
        classroomPageList.setDataList(classroomList);
        classroomPageList.setConditionMap(conditionMap);
        classroomPageList.setPage(page);
        classroomPageList.setPageSize(pageSize);
        classroomPageList.setSort(sort);
        classroomPageList.setTotal(total);
        classroomPageList.setPages(pages);
        return classroomPageList;
    }

    /**
     * 拿到保洁课堂表中的所有不同的标签
     * @return
     * @Description:
     */
    public List<String> findAllSign() {
        return classroomMapper.findAllSign();
    }
}
